package com.hxkj.client.service.impl;


import com.hxkj.client.service.*;

import com.hxkj.domain.enums.TemplteExceptionEnum;
import com.hxkj.domain.po.FreeMarker;
import com.hxkj.domain.po.Templet;
import com.hxkj.domain.po.WebsiteAddr;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Date;

/***
 * 分页标签封装类
 * @author yaosy
 * @date 2018年8月20日15:42:18
 * ***/
@Service
@Slf4j
public class PagingServiceImpl implements PagingService {


    @Autowired
    private TemplteServiceClient templteServiceClient;

    @Value("${storage.sftp.fileRoot}")
    private String fileRoot;

    @Autowired
    private WebsiteAddrServiceClient websiteAddrServiceClient;

    @Value("${cms.tmpRes}")
    private String tmpRes;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String KEY = "WEBSITE_ADD";

    /***
     * 异步处理freemarker校验
     * **/
    @Async
    protected void checkDirective(FreeMarker freeMarker) throws Exception {
        if (freeMarker == null) {
            throw new CustomException(TemplteExceptionEnum.TEMPLTE_DATA_BLANK);
        }
        if (freeMarker.getSiteId() == null) {
            throw new CustomException(TemplteExceptionEnum.TEMPLTE_NOT_SITEID);
        }
    }

    /*
     * 获取url规则配置
     * **/
    //如果redis换成不存在;则从数据库中查询;如若存在;则从缓存中获取
    protected WebsiteAddr getWebSiteAddr(FreeMarker freeMarker) throws Exception {
        WebsiteAddr websiteAddr = (WebsiteAddr) redisTemplate.opsForValue().get(KEY + freeMarker.getSiteId());
        if (websiteAddr != null) return websiteAddr;
        WebsiteAddr websiteAddrData = new WebsiteAddr();
        websiteAddrData.setDelTag(0L);
        websiteAddrData.setSiteId(freeMarker.getSiteId());
        return websiteAddrServiceClient.get(websiteAddrData).getData();
    }

    /******************************************获取模板*********************************************************************************/
    protected ResultDataStruct<Templet> getTemplet(FreeMarker freeMarker) throws Exception {
        Templet templet = new Templet();
        templet.setTempletId(freeMarker.getTempletId());
        templet.setDelTag((byte) 0);
        ResultDataStruct<Templet> templetResultDataStruct = templteServiceClient.get(templet);
        return templetResultDataStruct;
    }


    /********************************************************分页生成地址规则************************************************************/
    public String pageFreeMaker(FreeMarker freeMarker) throws Exception {
        String uploadPath = "";
        String filePath = System.getProperty("java.io.tmpdir") + File.separator + tmpRes;
        try {
            ResultDataStruct<Templet> resultDataStruct = getTemplet(freeMarker);
            Templet templetData = resultDataStruct.getData();
            File htmlFile = new File(filePath + templetData.getTempletName());
            WebsiteAddr websiteAddr = getWebSiteAddr(freeMarker);
            String parentName = websiteAddr.getUrlPrefix();
            //生成文件html文件,并上传到sftp
            String filename = "";
            String urlRegularPath = DateUtils.toDateToString(new Date(), websiteAddr.getUrlRegular());
            if (freeMarker.getArticleId() != null && !freeMarker.getAliasName().equals("index")) {//文章
                filename = String.valueOf(freeMarker.getSiteId()) + freeMarker.getArticleId() + "";
                uploadPath = parentName + File.separator + DateUtils.toDateToString(new Date(), websiteAddr.getUrlRegular()) + File.separator + filename;
            } else {//频道名称
                if (freeMarker.getAliasName().equals("index")) {//首页
                    filename = "index";
                    uploadPath = parentName + File.separator + filename;
                } else {//其他频道
                    if (freeMarker.getPageNum() != null)
                        //区分了分类列表分页和tag详情列表分页
                        if (freeMarker.getStatus() == 6L) {//tag详情列表
                            filename = "tag" + String.valueOf(freeMarker.getSiteId()) + freeMarker.getTagsId() + "_" + freeMarker.getPageNum();
                        } else {
                            filename = String.valueOf(freeMarker.getSiteId()) + freeMarker.getColumnId() + "_" + freeMarker.getPageNum();
                        }
                    else
                        filename = String.valueOf(freeMarker.getSiteId()) + freeMarker.getColumnId() + "";
                    uploadPath = parentName + File.separator + urlRegularPath + File.separator + filename;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadPath;
    }
}
